var app = getApp()
Page({
  data: {
    fileType: 0,
    fileName: "请点击选择文件",
    path: "您还未选择打印文件",
    notice: "",
    way: "单面",
    count: "0",
    pageCount: "",
    mobile: "",
    wechat: "",
    qq: '',
    name: "",
    msg: "",
    type:"进行中",
    upTime:0
  },upload: function () {
    var that = this
    wx.chooseMessageFile({
      success(res) {
        res.tempFiles.forEach(element => {
          var str = element.path.replace(/.+\./, "").toLowerCase();
          var tmpType = 0;
          if (str.indexOf("doc") != -1) {
            tmpType = 1
          } else if (str.indexOf("xls") != -1) {
            tmpType = 2
          } else {
            tmpType = 0
          }
          that.setData({ fileType: tmpType, fileName: element.name, path: element.path })
        });
      }
    })
  },onLoad: function (options) {
    this.setData({openId:app.globalData.openId})
  }, submit: function () {
   if(app.globalData.hasLogin){
    var that=this
    if (app.globalData.openId != null) {
      if (that.data.fileName == "请点击选择文件") {
        wx.showToast({ title: '请选择文件', icon: 'error', duration: 1000 })
        return
      }
      if (that.data.count == "0" || that.data.count == "") {
        wx.showToast({ title: '请输入打印份数', icon: 'error', duration: 1000 })
        return
      }
      if (that.data.name == "") {
        wx.showToast({ title: '请输入收货人', icon: 'error', duration: 1000 })
        return
      }
      if (that.data.mobile == ""||that.data.mobile.length!=11) {
        wx.showToast({ title: '手机号错误', icon: 'error', duration: 1000 })
        return
      }
      var stamp = Date.parse(new Date()).toString()
      wx.cloud.uploadFile({ cloudPath: that.data.fileName+"_"+stamp, filePath:that.data.path }).then(res => {
      that.setData({ 
        fileId:res.fileID,
         upTime:Date.parse(new Date()) / 1000
      })
        wx.cloud.callFunction({ name: 'function', data: { type: "post", data: that.data } }).then(res1 => {
          wx.showToast({ title: '已提交预约', icon: 'success', duration: 1000 })
          that.setData({
          fileType: 0,
          fileName: "请点击选择文件",
          path: "您还未选择打印文件",
          notice: "",
          way: "单面",
          count: "0",
          pageCount: "",
          mobile: "",
          wechat: "",
          qq: '',
          name: "",
          msg: "",
          type:"进行中"
         })
         that.onLoad();
        })
      })
    } else {
      wx.showToast({ title: '请先登录', icon: 'error', duration: 1000 })
    }
   }else{
    wx.showToast({ title: '请先登录', icon: 'error', duration: 1000 })
   }

  }, wayTap: function () {
    if (this.data.way == "单面") {
      this.setData({ way: "双面" })
    } else {
      this.setData({ way: "单面" })
    }
  }, watchCount: function (a) {
    if (a.detail.value != "") {
      this.setData({ count: this.validateNumber(a.detail.value) })
    } else {
      this.setData({ count: "0" })
    }
  }, watchPageCount: function (a) {
    this.setData({ pageCount: this.validateNumber(a.detail.value) })
  }, watchName: function (a) {
    this.setData({ name: a.detail.value })
  }, watchMobile: function (a) {
    this.setData({ mobile: a.detail.value })
  }, watchWeChat: function (a) {
    this.setData({ wechat: a.detail.value })
  }, watchQQ: function (a) {
    this.setData({ qq: a.detail.value })
  }, watchMsg: function (a) {
    this.setData({ msg: a.detail.value })
  },validateNumber(val) {
    return val.replace(/\D/g, '')
  },onShow(){
    var that = this
    wx.cloud.callFunction({ name: 'function', data: { type: "getNotice" } }).then(res => { that.setData({ notice: res.result.data[0].notice }) })
  },onShareAppMessage(){

  },onShareTimeline(){
    
  }
})